from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy import Integer, String, ForeignKey
from ..database.db import Base  # 假设你有一个基础模型类 Base


class FolderSources(Base):
    """
    文件夹资源关联表的 ORM 映射
    """
    __tablename__ = 'folder_sources'  # 表名

    # 字段定义
    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)  # 主键，自动递增
    folder_id: Mapped[int] = mapped_column(
        ForeignKey('folders.id', ondelete='CASCADE', onupdate='RESTRICT'),  # 设置外键及级联操作
        nullable=False  # 不允许为空
    )
    resource_type: Mapped[str] = mapped_column(String(255), nullable=False)  # 资源类型
    resource_id: Mapped[int] = mapped_column(Integer, nullable=False)  # 资源 ID

    # 关系定义（可选）
    folder= relationship("Folder", back_populates="sources")  # 与 Folder 模型的关联关系

    def as_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}